package edu.gmu.projects.fireresponder.gui;

import java.util.Locale;

import android.app.Activity;
import android.app.AlertDialog;
import android.content.DialogInterface;
import android.os.Bundle;
import android.preference.PreferenceManager;
import android.speech.tts.TextToSpeech;

public class NotificationDialogActivity extends Activity implements
		TextToSpeech.OnInitListener {
	public final static String DIALOG_MESSAGE = ".NotificationDialogActivity.DIALOG_MESSAGE";
	private TextToSpeech mTts;
	private boolean useTtsPref = false;

	@Override
	public void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);

		Bundle extras = getIntent().getExtras();
		String message = extras.getString(DIALOG_MESSAGE);

		if (message != null) {
			AlertDialog.Builder builder = new AlertDialog.Builder(this);

			builder.setMessage(message).setCancelable(false).setPositiveButton(
					"OK", new DialogInterface.OnClickListener() {
						public void onClick(DialogInterface dialog, int id) {
							dialog.cancel();
							NotificationDialogActivity.this.finish();
						}
					});

			AlertDialog dialog = builder.create();
			dialog.show();

			useTtsPref = PreferenceManager.getDefaultSharedPreferences(
					getApplicationContext()).getBoolean(
					SituationMapPreferences.USE_TTS_PREF, false);

			if (useTtsPref == true) {
				mTts = new TextToSpeech(this, this);
			}
		}
	}

	@Override
	public void onDestroy() {
		if (mTts != null) {
			mTts.stop();
			mTts.shutdown();
		}

		super.onDestroy();
	}

	public void onInit(int status) {
		if (status == TextToSpeech.SUCCESS) {
			mTts.setLanguage(Locale.US);
			Bundle extras = getIntent().getExtras();

			if (extras != null) {
				String message = extras.getString(DIALOG_MESSAGE);
				mTts.speak(message, TextToSpeech.QUEUE_FLUSH, null);
			}
		}
	}
}
